//
//  BtnCollectionView.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/4.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "BtnCollectionView.h"
#import "BtnCollectionViewCell.h"
#import "TbOrderWebViewController.h"
#import <AlibcTradeSDK/AlibcTradeSDK.h>
#import "SecondNavModel.h"
#import "JDKeplerManager.h"
static NSString *btnViewCellID = @"btnViewCellID";

@interface BtnCollectionView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation BtnCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    
    self = [super initWithFrame:frame collectionViewLayout:layout];
    
    if (self) {
        
        self.delegate = self;
        self.dataSource = self;
        
        [self registerClass:[BtnCollectionViewCell class] forCellWithReuseIdentifier:btnViewCellID];
        
    }
    
    return self;
}



#pragma mark -collectionView delegate、datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BtnCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:btnViewCellID forIndexPath:indexPath];
    SecondNavModel *model = self.dataArray[indexPath.row];
    cell.nameL.text = model.title;
    [cell.iconIV sd_setImageWithURL:[NSURL URLWithString:model.photo]];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//0  h5  1原生 2淘宝SDKh5  3拉起淘宝SDK
    SecondNavModel *model = self.dataArray[indexPath.row];
    
    
    switch (model.type) {
        case 0:
        {
            WebViewController *webview = [[WebViewController alloc] init];
            webview.url = model.url;
        }
            break;
        case 1:
        {
            
        }
            break;
        case 7:
        {
            [[JDKeplerManager sharedManager] jumpJDUrl:model.url Controller:self.parentVC];
        }
            break;
        case 6:
        {
            
            switch (model.tksdk) {
                case 1:
                    {
                        [[JDKeplerManager sharedManager] jumpToTheOrderListPageController:self.parentVC];
                    }
                    break;
                case 2:
                {
                    [[JDKeplerManager sharedManager] jumpToTheShoppingCartPageController:self.parentVC];
                }
                    break;
                case 3:
                {
                    [[JDKeplerManager sharedManager] jumpToTheNavigationCategoriesPageController:self.parentVC];
                }
                    break;
                case 4:
                {
                    [[JDKeplerManager sharedManager] jumpToTheHomePageController:self.parentVC];
                }
                    break;
                    
                default:
                    break;
            }
            
        }
            break;
        case 2:
        {
            //根据链接打开页面
            id<AlibcTradePage> page = [AlibcTradePageFactory page:model.url];
            
            AlibcTradeTaokeParams *taoKeParams=[[AlibcTradeTaokeParams alloc] init];
            taoKeParams.pid= nil;
            //打开方式
            AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
            showParam.openType = AlibcOpenTypeAuto;
            [[AlibcTradeSDK sharedInstance].tradeService show: self.parentVC.navigationController page:page showParams:showParam taoKeParams: nil trackParam: nil tradeProcessSuccessCallback:nil tradeProcessFailedCallback:nil];
        }
            break;
        case 3:
        {
            TbOrderWebViewController *vc = [[TbOrderWebViewController alloc] init];
            id<AlibcTradePage> page = [AlibcTradePageFactory myOrdersPage:0 isAllOrder:YES];
            id<AlibcTradePage> page1 = [AlibcTradePageFactory myCartsPage];
            
            AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
            showParam.openType = AlibcOpenTypeH5;
            showParam.backUrl = @"tbopen25027070";
            showParam.isNeedPush = YES;
            showParam.nativeFailMode = AlibcNativeFailModeJumpH5;
            
            if (self.supportType==2) {
                if (model.tksdk==1) {
                    vc.title = @"淘宝订单";
                    // 优先拉起手淘：@"taobao_scheme" \\\ 天猫：tmall_scheme
                    showParam.linkKey = @"taobao_scheme";
                    
                    [self gotoOrderWebViewWith:vc page:page andshowParams:showParam];
                }
                if (model.tksdk==2) {
                    // 淘宝
                    vc.title = @"购物车";
                    // 优先拉起手淘：@"taobao_scheme" \\\ 天猫：tmall_scheme
                    showParam.linkKey = @"taobao_scheme";
                    [self gotoOrderWebViewWith:vc page:page1 andshowParams:showParam];
                }
            }
            
            if (self.supportType==3) {
                if (model.tksdk==1) {
                    vc.title = @"天猫订单";
                    // 优先拉起手淘：@"taobao_scheme" \\\ 天猫：tmall_scheme
                    showParam.linkKey = @"tmall_scheme";
                    
                    [self gotoOrderWebViewWith:vc page:page andshowParams:showParam];
                }
                
                if (model.tksdk==2) {
                    vc.title = @"购物车";
                    showParam.linkKey = @"tmall_scheme";
                    [self gotoOrderWebViewWith:vc page:page1 andshowParams:showParam];

                }
            }
            
        }
            break;
            
        default:
            break;
    }
    
}

- (void)reloadDataWithDataArray:(NSArray *)array {
    self.dataArray = array;
    [self reloadData];
}

- (void)gotoOrderWebViewWith:(TbOrderWebViewController *)vc page:(id<AlibcTradePage>)page andshowParams:(AlibcTradeShowParams *)showParams {
    
    NSInteger res = [[AlibcTradeSDK sharedInstance].tradeService show:vc webView:vc.webView page:page showParams:showParams taoKeParams:nil trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
        
    } tradeProcessFailedCallback:^(NSError * _Nullable error) {
        
    }];
    
    if (res) {
        
        [self.parentVC.navigationController pushViewController:vc animated:YES];
        
    }
    
}

@end
